home *** CD-ROM | disk | FTP | other *** search
- /****************************************/
- /* "AEHandler" source code */
- /* Copyright 1994, Dave Mark. */
- /* This code is free to one and all... */
- /****************************************/
-
-
- #include <GestaltEqu.h>
- #include <AppleEvents.h>
-
-
- #define kBaseResID 128
- #define kErrorALRTid 128
- #define kWINDResID 128
-
- #define kVisible true
- #define kMoveToFront (WindowPtr)-1L
- #define kSleep 60L
- #define kNilFilterProc 0L
- #define kGestaltMask 1L
- #define kKeepInSamePlane false
-
- #define kWindowStartX 20
- #define kWindowStartY 50
-
- #define mApple kBaseResID
- #define iAbout 1
-
- #define mFile kBaseResID+1
- #define iClose 1
- #define iQuit 3
-
-
- /*************/
- /* Globals */
- /*************/
-
- Boolean gDone;
- short gNewWindowX = kWindowStartX,
- gNewWindowY = kWindowStartY;
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolboxInit( void );
- void MenuBarInit( void );
- void AEInit( void );
- void AEInstallHandlers( void );
- pascal OSErr DoOpenApp( AppleEvent *event, AppleEvent *reply, long refcon );
- pascal OSErr DoOpenDoc( AppleEvent *event, AppleEvent *reply, long refcon );
- OSErr CheckForRequiredParams( AppleEvent *event );
- pascal OSErr DoPrintDoc( AppleEvent *event, AppleEvent *reply, long refcon );
- pascal OSErr DoQuitApp( AppleEvent *event, AppleEvent *reply, long refcon );
- void OpenDocument( FSSpec *fileSpecPtr );
- WindowPtr CreateWindow( Str255 name );
- void DoCloseWindow( WindowPtr window );
- void EventLoop( void );
- void DoEvent( EventRecord *eventPtr );
- void HandleMouseDown( EventRecord *eventPtr );
- void HandleMenuChoice( long menuChoice );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void DoUpdate( EventRecord *eventPtr );
- void DoError( Str255 errorString );
-
-
- /******************************** main *********/
-
- void main( void )
- {
- ToolboxInit();
- MenuBarInit();
- AEInit();
-
- EventLoop();
- }
-
-
- /*********************************** ToolboxInit */
-
- void ToolboxInit( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- }
-
-
- /****************** MenuBarInit ***********************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar = GetNewMBar( kBaseResID );
-
- if ( menuBar == NULL )
- DoError( "\pCouldn't load the MBAR resource..." );
-
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
- }
-
-
- /******************************** AEInit *********/
-
- void AEInit( void )
- {
- OSErr err;
- long feature;
-
- err = Gestalt( gestaltAppleEventsAttr, &feature );
-
- if ( err != noErr )
- DoError( "\pError returned by Gestalt!" );
-
- if ( !( feature & ( kGestaltMask << gestaltAppleEventsPresent ) ) )
- DoError( "\pThis configuration does not support Apple events..." );
-
- AEInstallHandlers();
- }
-
-
- /******************************** AEInstallHandlers *********/
-
- void AEInstallHandlers( void )
- {
- OSErr err;
-
- err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc( DoOpenApp ), 0L, false );
-
- if ( err != noErr )
- DoError( "\pError installing 'oapp' handler..." );
-
- err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc( DoOpenDoc ), 0L, false );
-
- if ( err != noErr )
- DoError( "\pError installing 'odoc' handler..." );
-
- err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc( DoPrintDoc ), 0L, false );
-
- if ( err != noErr )
- DoError( "\pError installing 'pdoc' handler..." );
-
- err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc( DoQuitApp ), 0L, false );
-
- if ( err != noErr )
- DoError( "\pError installing 'quit' handler..." );
- }
-
-
- /****************** DoOpenApp ***********************/
-
- pascal OSErr DoOpenApp( AppleEvent *event, AppleEvent *reply, long refcon )
- {
- OpenDocument( nil );
-
- return noErr;
- }
-
-
- /****************** DoOpenDoc ***********************/
-
- pascal OSErr DoOpenDoc( AppleEvent *event, AppleEvent *reply, long refcon )
- {
- OSErr err;
- FSSpec fileSpec;
- long i, numDocs;
- DescType returnedType;
- AEKeyword keywd;
- Size actualSize;
- AEDescList docList = { typeNull, nil };
-
- // get the direct parameter--a descriptor list--and put
- // it into docList
- err = AEGetParamDesc( event, keyDirectObject,
- typeAEList, &docList);
-
- // check for missing required parameters
- err = CheckForRequiredParams( event );
- if ( err )
- {
- // an error occurred: do the necessary error handling
- err = AEDisposeDesc( &docList );
- return err;
- }
-
- // count the number of descriptor records in the list
- // should be at least 1 since we got called and no error
- err = AECountItems( &docList, &numDocs );
-
- if ( err )
- {
- // an error occurred: do the necessary error handling
- err = AEDisposeDesc( &docList );
- return err;
- }
-
- // now get each descriptor record from the list, coerce
- // the returned data to an FSSpec record, and open the
- // associated file
- for ( i=1; i<=numDocs; i++ )
- {
- err = AEGetNthPtr( &docList, i, typeFSS, &keywd,
- &returnedType, (Ptr)&fileSpec,
- sizeof( fileSpec ), &actualSize );
-
- OpenDocument( &fileSpec );
- }
-
- err = AEDisposeDesc( &docList );
-
- return err;
- }
-
-
- /****************** CheckForRequiredParams ***********************/
-
- OSErr CheckForRequiredParams( AppleEvent *event )
- {
- DescType returnedType;
- Size actualSize;
- OSErr err;
-
- err = AEGetAttributePtr( event, keyMissedKeywordAttr,
- typeWildCard, &returnedType,
- nil, 0, &actualSize);
-
- if ( err == errAEDescNotFound ) // you got all the required
- //parameters
- return noErr;
- else
- if ( err == noErr ) // you missed a required parameter
- return errAEParamMissed;
- else // the call to AEGetAttributePtr failed
- return err;
- }
-
-
- /****************** DoPrintDoc ***********************/
-
- pascal OSErr DoPrintDoc( AppleEvent *event, AppleEvent *reply, long refcon )
- {
- return noErr;
- }
-
-
- /****************** DoQuitApp ***********************/
-
- pascal OSErr DoQuitApp( AppleEvent *event, AppleEvent *reply, long refcon )
- {
- SysBeep( 20 );
- gDone = true;
-
- return noErr;
- }
-
-
- /****************** OpenDocument ***********************/
-
- void OpenDocument( FSSpec *fileSpecPtr )
- {
- WindowPtr window;
-
- if ( fileSpecPtr == nil )
- window = CreateWindow( "\p<Untitled>" );
- else
- window = CreateWindow( fileSpecPtr->name );
- }
-
-
- /****************** CreateWindow ***********************/
-
- WindowPtr CreateWindow( Str255 name )
- {
- WindowPtr window;
- short windowWidth, windowHeight;
-
- window = GetNewWindow( kWINDResID, nil, kMoveToFront );
-
- SetWTitle( window, name );
-
- MoveWindow( window, gNewWindowX, gNewWindowY, kKeepInSamePlane );
-
- gNewWindowX += 20;
- windowWidth = window->portRect.right - window->portRect.left;
-
- if ( gNewWindowX + windowWidth > qd.screenBits.bounds.right )
- {
- gNewWindowX = kWindowStartX;
- gNewWindowY = kWindowStartY;
- }
-
- gNewWindowY += 20;
- windowHeight = window->portRect.bottom - window->portRect.top;
-
- if ( gNewWindowY + windowHeight > qd.screenBits.bounds.bottom )
- {
- gNewWindowX = kWindowStartX;
- gNewWindowY = kWindowStartY;
- }
-
- ShowWindow( window );
- SetPort( window );
-
- return window;
- }
-
-
- /****************** DoCloseWindow ***********************/
-
- void DoCloseWindow( WindowPtr window )
- {
- if ( window != nil )
- DisposeWindow( window );
- }
-
-
- /******************************** EventLoop *********/
-
- void EventLoop( void )
- {
- EventRecord event;
-
- gDone = false;
- while ( gDone == false )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
- DoEvent( &event );
- }
- }
-
-
- /************************************* DoEvent */
-
- void DoEvent( EventRecord *eventPtr )
- {
- char theChar;
-
- switch ( eventPtr->what )
- {
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- case updateEvt:
- DoUpdate( eventPtr );
- break;
- case kHighLevelEvent:
- AEProcessAppleEvent( eventPtr );
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick( eventPtr, window );
- break;
- case inGoAway:
- if ( TrackGoAway( window, eventPtr->where ) )
- DoCloseWindow( window );
- break;
- case inContent:
- SelectWindow( window );
- break;
- case inDrag :
- DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
- break;
- }
- }
-
-
- /****************** HandleMenuChoice ***********************/
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch ( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /****************** HandleAppleChoice ***********************/
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- SysBeep( 20 );
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /****************** HandleFileChoice ***********************/
-
- void HandleFileChoice( short item )
- {
- switch ( item )
- {
- case iClose:
- DoCloseWindow( FrontWindow() );
- break;
- case iQuit:
- gDone = true;
- break;
- }
- }
-
-
- /************************************* DoUpdate */
-
- void DoUpdate( EventRecord *eventPtr )
- {
- WindowPtr window;
-
- window = (WindowPtr)eventPtr->message;
-
- BeginUpdate(window);
- EndUpdate(window);
- }
-
-
- /***************** DoError ********************/
-
- void DoError( Str255 errorString )
- {
- ParamText( errorString, "\p", "\p", "\p" );
-
- StopAlert( kErrorALRTid, kNilFilterProc );
-
- ExitToShell();
- }